//
//  CZQuestionController.m
//  A01-CZ彩票
//
//  Created by apple on 15/6/17.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "CZQuestionController.h"
#import "CZQuestion.h"
#import "CZQuestionContentController.h"
@interface CZQuestionController ()
@property (nonatomic, strong) NSArray *questions;
@end

@implementation CZQuestionController

//懒加载
- (NSArray *)questions{
    if (_questions == nil) {
        //1 加载json文件
        NSString *path = [[NSBundle mainBundle] pathForResource:@"help.json" ofType:nil];
        NSData *data = [NSData dataWithContentsOfFile:path];
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        
        //2 字典转模型
        NSMutableArray *mArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            CZQuestion *question = [CZQuestion questionWithDic:dic];
            [mArray addObject:question];
        }
        //
        _questions = mArray.copy;
    }
    return _questions;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    
    self.title = @"常见问题";
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, -1)];
}

- (void)setData{
    //把question 转换成czitem对象
    NSMutableArray *mArray = [NSMutableArray array];
    for (CZQuestion *question in self.questions) {
        CZItem *item = [CZItemArrow itemWithTitle:question.title icon:nil option:^{
            //打开控制器
            CZQuestionContentController *vc = [CZQuestionContentController new];
            vc.question = question;
            
            //关闭按钮
            UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeClick)];
            vc.navigationItem.leftBarButtonItem = item;
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
            [self presentViewController:nav animated:YES completion:nil];
        }];
        [mArray addObject:item];
    }
    
    
    CZGroup *group = [CZGroup groupWithItems:mArray.copy];
    self.groups = @[group];
}

- (void)closeClick{
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end
